package gptma.bo.dao.querier;

import java.util.ArrayList;
import java.util.List;

public abstract class CriteriaListHolder {

	private List<SearchCriterion> criteria;

	public CriteriaListHolder() {
		this.criteria = new ArrayList<SearchCriterion>();
	}

	/**
	 * Adds a new criterion to the set, searchMode: equals
	 * 
	 * @param property
	 *            property
	 * @param value
	 *            value
	 * @return criterionSet instance
	 */
	public CriteriaListHolder addCriterion(String property, Object value) {
		criteria.add(new SearchCriterion(property, value));
		return this;
	}

	/**
	 * Adds a new criterion to the set
	 * 
	 * @param property
	 *            property
	 * @param searchMode
	 *            searchMode
	 * @param value
	 *            value
	 * @return criterionSet instance
	 */
	public CriteriaListHolder addCriterion(String property,
			SearchMode searchMode, Object value) {
		criteria.add(new SearchCriterion(property, searchMode, value));
		return this;
	}

	/**
	 * Adds a new criterion to the set, searchMode: equals, case insensitive
	 * 
	 * @param property
	 *            property
	 * @param value
	 *            value
	 * @return criterionSet instance
	 */
	public CriteriaListHolder addCaseInsensitiveCriterion(String property,
			String value) {
		criteria.add(new SearchCriterion(property, value, false));
		return this;
	}

	/**
	 * Adds a new criterion to the set, case insensitive
	 * 
	 * @param property
	 *            property
	 * @param searchMode
	 *            searchMode
	 * @param value
	 *            value
	 * @return criterionSet instance
	 */
	public CriteriaListHolder addCaseInsensitiveCriterion(String property,
			SearchMode searchMode, String value) {
		criteria.add(new SearchCriterion(property, searchMode, value, false));
		return this;
	}

	/**
	 * Sets the global case-sensitivity settings for this criterion set. This
	 * setting only applies to string values (ignored for the others) This
	 * method must be called after every criterion has been added to the set,
	 * otherwise, it won't be effective for criterion that are added later. By
	 * default, the set is case sensitive, so this method won't do anything if
	 * true is passed as a parameter.
	 * 
	 * For individual case-sensitivity settings, use corresponding methods
	 * 
	 * @param caseSensitive
	 *            caseSensitivity
	 */
	public CriteriaListHolder setCaseSensitive(boolean caseSensitive) {
		for (SearchCriterion criterion : criteria) {
			if (criterion.getValue() instanceof String) {
				criterion.setCaseSensitive(caseSensitive);
			}
		}

		return this;
	}

	/**
	 * For test purposes only
	 * 
	 * @param property
	 *            searched property
	 * @param value
	 *            searched value
	 * @return true if the given criteria has been set for this object
	 */
	public boolean containsCriterion(String property, Object value) {
		return containsCriterion(property, SearchMode.EQUALS, value);
	}

	/**
	 * For test purposes only
	 * 
	 * @param property
	 *            searched property
	 * @param searchMode
	 *            searched mode
	 * @param value
	 *            searched value
	 * @return true if the given criteria has been set for this object
	 */
	public boolean containsCriterion(String property, SearchMode searchMode,
			Object value) {
		SearchCriterion searched = new SearchCriterion(property, searchMode,
				value);
		return criteria.contains(searched);
	}

	/**
	 * @return the criteria
	 */
	public List<SearchCriterion> getCriteria() {
		return criteria;
	}

	/**
	 * toString
	 * 
	 * @return a string representation of the object
	 */
	@Override
	public String toString() {
		return criteria.toString();
	}
}
